﻿using System;
using System.Collections.Generic;
using System.Text;
using NBearLite;
using CommonUtility.Utility;
using DataAccess;
using System.Data;

namespace UIDevCore
{
    public class UIUpdateHelper
    {
        #region 构造器
        public UIUpdateHelper(IQueryTable table)
        {
            this.DB = DBHelper.DB;
            this._Table = table;
            this.Section = this.DB.Save(table);
        }

        public UIUpdateHelper(IQueryTable table, EDatabase db)
        {
            this.DB = db;
            this._Table = table;
            this.Section = this.DB.Save(table);
        }

        public UIUpdateHelper(IQueryTable table, string postData): this(table)
        {
            if(!string.IsNullOrEmpty(postData))
                this._Values = JsonHelper.DeserializeData<Dictionary<String, Object>>(postData);
        }

        public UIUpdateHelper(IQueryTable table, string postData, EDatabase db) : this(table, db)
        {
            if (!string.IsNullOrEmpty(postData))
                this._Values = JsonHelper.DeserializeData<Dictionary<String, Object>>(postData);
        }
        #endregion

        #region Properties
        public EDatabase DB;

        public SaveSqlSection Section { get; set; }

        private Dictionary<String, Object> _Values { get; set; }

        private IQueryTable _Table;

        private bool _IsInsert = true;
        public bool IsInsert
        {
            get { return _IsInsert; }
        }

        public bool HasValues { get { return this._Values != null && this._Values.Count > 0; } }
        #endregion

        #region Save Method
        public UIUpdateHelper AddUpdateColumn(params QueryColumn[] columns)
        {
            foreach (QueryColumn column in columns)
            {
                object value = this.GetValue(column);
                if (value != null)
                    this.Section.AddColumn(column, value);
            }
            return this;
        }

        public UIUpdateHelper AddUpdateColumn(QueryColumn column, object value)
        {
            this.Section.AddColumn(column, value);
            return this;
        }

        public bool Save()
        {
           long key  = this.GetValue<long>(this._Table.IDColumn, 0);
           if (key > 0)
           {
               _IsInsert = false;
               return this.Save(this._Table.IDColumn == key);
           }

           return this.Section.Execute() == 1;
        }

        public bool Save(WhereClip where)
        {
            return this.Section.Where(where).Execute() == 1;
        }

        public long SaveAndGetKey()
        {
            long key = this.GetValue<long>(this._Table.IDColumn, 0);
            if (key > 0)
                return this.Section.Execute() == 1 ? key : 0;
            else
            {
                object id = this.InsertAndGetKey();
                if (id != null)
                    Convert.ToInt64(id);
                return 0;
            }
        }

        //public long InsertAndGetKey()
        //{
        //    return Convert.ToInt64(this.Section.ExecuteReturnAutoIncrementID(this._Table.IDColumn));
        //}

        public object InsertAndGetKey()
        {
            return this.Section.ExecuteReturnAutoIncrementID(this._Table.IDColumn);
        }
        #endregion

        #region GetValue
        public object GetValue(QueryColumn column)
        {
            return this.GetValue(column.ColumnName);
        }

        public object GetValue(string paramName)
        {
            return UIHelper.getDicValue(paramName, this._Values);
        }

        public T GetValue<T>(QueryColumn column, T defaultValue)
        {
            return this.GetValue<T>(column.ColumnName,defaultValue);
        }

        public T GetValue<T>(string paramName, T defaultValue)
        {
            return UIHelper.getDicValue<T>(paramName, this._Values, defaultValue);
        }
        #endregion

        #region UpdateDatas
        /// <summary>
        /// 批量更新DataTable,(默认一次处理10条数据)
        /// </summary>
        /// <param name="dt">更新的DataTable</param>
        /// <returns></returns>
        public bool UpdateDatas(DataTable updateData, WhereClip where, params OrderByClip[] orderBys)
        {
            return UpdateDatas(updateData, 10, where, orderBys);
        }

        /// <summary>
        /// 批量更新DataTable
        /// </summary>
        /// <param name="dt">更新的DataTable</param>
        /// <param name="batchSize">一次处理的数据条数</param>
        /// <returns></returns>
        public bool UpdateDatas(DataTable updateData, int batchSize, WhereClip where, params OrderByClip[] orderBys)
        {
            SelectSqlSection select = this.DB.Select(this._Table).Where(where).OrderBy(orderBys);
            return this.DB.Save(select.ToDbCommand(), batchSize, updateData) > 0;
        }
        #endregion
    }
}
